/*
 *
 * Created on June 20, 2009, 4:30 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package fhv.itb.mst2955.client.ui;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
import com.google.gwt.gears.client.Factory;
import com.google.gwt.gears.client.GearsException;
import com.google.gwt.gears.client.database.Database;
import com.google.gwt.gears.client.localserver.LocalServer;
import com.google.gwt.gears.client.localserver.ManagedResourceStore;
import com.google.gwt.gears.offline.client.Offline;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;


/**
 * The application's entry point.
 */
public class AddressBookMain implements EntryPoint {

    private final int MAIN_TAB = 0;
    private final int CONTACT_TAB = 1;
    private final int INVOICES_TAB = 2;

    private static Database db;
    private TabPanel mainPanel;
    private ContactTab contactsTab;
    private final Button createManagedResourceStoreButton = new Button(
            "Go Offline");
    private final Button removeManagedResourceStoreButton = new Button(
            "Remove Offline Store");


    public AddressBookMain() {
        // create the local database
        db = Factory.getInstance().createDatabase();
        db.open("addressbook-database");
    }


    public void onModuleLoad() {
        mainPanel = new TabPanel();
        contactsTab = new ContactTab();

        mainPanel.addBeforeSelectionHandler(getBeforeSelHandler);
        mainPanel.add(new HTML("<div>Main Page of Address Book Application</div>"), "Home");
        mainPanel.add(contactsTab, "Contacts");
        mainPanel.selectTab(CONTACT_TAB);
        RootPanel.get().add(mainPanel);

//        RootPanel.get().add(createManagedResourceStoreButton);
//        RootPanel.get().add(removeManagedResourceStoreButton);
//        RootPanel.get().add(statusLabel);
        createManagedResourceStore();

        createManagedResourceStoreButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                createManagedResourceStore();
            }
        });

        removeManagedResourceStoreButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                try {
                    LocalServer server = Factory.getInstance().createLocalServer();
                    ManagedResourceStore store = Offline.getManagedResourceStore();
                    server.removeManagedStore(store.getName());
                } catch (GearsException e) {
 
                }
            }
        });
    }


    /**
     * Creates the managed resource store, so that the URLs listed in the manifest file
     * will be available offline. Also, a timer is started, to regularly check if the offline
     * documents are up-to-date.
     */
    private void createManagedResourceStore() {
        try {
            final ManagedResourceStore managedResourceStore = Offline.getManagedResourceStore();

            new Timer() {

                final String oldVersion = managedResourceStore.getCurrentVersion();


                @Override
                public void run() {
                    switch (managedResourceStore.getUpdateStatus()) {
                        case ManagedResourceStore.UPDATE_OK:
                            break;
                        case ManagedResourceStore.UPDATE_CHECKING:
                        case ManagedResourceStore.UPDATE_DOWNLOADING:
                            schedule(500);
                            break;
                        case ManagedResourceStore.UPDATE_FAILED:
                            break;
                    }
                }
            }.schedule(500);

        } catch (GearsException e) {
            Window.alert(e.getMessage());
        }
    }


    final BeforeSelectionHandler getBeforeSelHandler = new BeforeSelectionHandler() {

        public void onBeforeSelection(BeforeSelectionEvent event) {
            if (event.getSource().equals(mainPanel.getTabBar().getTab(CONTACT_TAB))) {
                // create new client tab, so that the list will be updated
                contactsTab = new ContactTab();
            }
        }
    };


    public static Database getDb() {
        return db;
    }
}
